package me.raih.controller;

import me.raih.pojo.po.Admin;
import me.raih.pojo.po.Sales;
import me.raih.pojo.vo.ResultInfo;
import me.raih.service.AdminService;
import me.raih.service.MenuService;
import me.raih.service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;

/**
 * @Author Raih
 * @Time 2020/11/22  12:09
 *
 *
 * 控制后台管理员和售货员的登录功能
 */
@Controller
//@SessionAttributes(value = {"LOGIN_SESSION_FLAG"})
//@RequestMapping(value = "/admin")
public class LoginController {

    @Autowired
    AdminService adminService;
    @Autowired
    SalesService salesService;
    @Autowired
    ResultInfo resultInfo;

    @Autowired
    MenuService menuService;


    @RequestMapping(value = {"/admin/login"})
    public String toLogin(){

        return "login";
    }

    @RequestMapping(value = {"/sales/login"})
    public String toLogin2(){

        return "login";
    }

    /**
     * 管理员登录
     * @param admin
     * @param session
     * @return
     */
    @RequestMapping(value = "/admin/adminLoginCheck",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo loginCheck(Admin admin, HttpSession session){

        session.setAttribute("ADMIN_MENU_FLAG",menuService.menuChange());

        if(Objects.isNull(admin)){
            resultInfo.setMessage("出现错误，请联系管理员！");
        }else {
            Admin login = adminService.login(admin.getUsername(), admin.getPassword());
            if(Objects.nonNull(login)){
                resultInfo.setFlag(true);
                //保存登录信息
                session.setAttribute("ADMIN_LOGIN_SESSION_FLAG",login);
            }else {
                //resultInfo.setFlag(false);
                resultInfo.setMessage("用户名或密码错误，请重试");
            }
        }
        return resultInfo;
    }

    /**
     * 售货员登录
     * @param sales
     * @param session
     * @return
     */
    @RequestMapping(value = "/sales/salesLoginCheck",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo toLogin(Sales sales, HttpSession session){
        ResultInfo resultInfo = new ResultInfo();
        Sales login = salesService.login(sales.getUsername(), sales.getPassword());
        session.setAttribute("SALES_MENU_FLAG",menuService.salesMenuChange());
        if(login != null){
            session.setAttribute("SALES_LOGIN_SESSION_FLAG",login);
            resultInfo.setFlag(true);
        }else {
            resultInfo.setFlag(false);
            resultInfo.setMessage("请检查用户名和密码");
        }
        return resultInfo;
    }

    /**
     * 管理员、售货员退出登录
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/LogOut")
    public void loginOut(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //删除所有session
        session.invalidate();

        response.sendRedirect("/admin/login");
    }


}
